﻿Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Content
Imports System.Globalization

Namespace Processors

    Public Class XMesh

        Private ModObject() As MapObjects.Models.ModelObject
        Private G As GraphicsDeviceManager
        Private ContentDir As String
        Public ModelLocation As Vector3
        Public ModelTexture As Texture2D
        Private MTLFile As String
        Private VertBuffer As VertexBuffer
        Private IndBuffer As IndexBuffer
        Dim VertArray() As VertexPositionNormalTexture
        Dim IndArray() As Integer

        Public Function GetModelObjects() As MapObjects.Models.ModelObject()
            Return ModObject
        End Function

        Public Sub New(Graphics As GraphicsDeviceManager, ContentDirectory As String)
            G = Graphics
            ContentDir = ContentDirectory
        End Sub

        Public Sub ImportXFile(ModelName As String, Scale As Single)
            Dim objReader As System.IO.StreamReader
            Dim OBJLine As String
            Dim s() As String
            Dim v As Vector3
            Dim vt As Vector2
            Dim f() As String
            Dim FS As System.IO.FileStream
            Dim MO As New MapObjects.Models.ModelObject
            Dim Positions = New List(Of Vector3)
            Dim Normals = New List(Of Vector3)
            Dim TexCoords = New List(Of Vector2)
            Dim Indices As New List(Of MapObjects.Models.IndexData)
            Dim Indices2 As New List(Of MapObjects.Models.IndexData)
            Dim ID As MapObjects.Models.IndexData
            Dim i As Integer, j As Integer
            Dim MOCt As Integer = -1
            Dim Textures() As Texture2D
            Dim Groups() As String
            Dim GrpCt As Integer = -1
            Dim VCt As Integer
            objReader = New System.IO.StreamReader(ContentDir & ModelName & ".mt2")
            Do While objReader.Peek <> -1
                GrpCt = GrpCt + 1
                OBJLine = objReader.ReadLine()
                s = Split(OBJLine, ",")
                ReDim Preserve Groups(GrpCt)
                ReDim Preserve Textures(GrpCt)
                Groups(GrpCt) = s(0)
                FS = New System.IO.FileStream(ContentDir & s(1), FileMode.Open)
                Textures(GrpCt) = Texture2D.FromStream(G.GraphicsDevice, FS)
                FS.Close()
            Loop
            objReader = Nothing
            objReader = New System.IO.StreamReader(ContentDir & ModelName & ".x")
            Do While objReader.Peek <> -1
                OBJLine = objReader.ReadLine()
                OBJLine = Replace(OBJLine, "  ", " ")

                If Mid(OBJLine, 1, 1) = "#" Or UCase(Mid(OBJLine, 1, 1)) = "S" Then
                ElseIf OBJLine = "" Then
                    Debug.Print(UCase(Mid(OBJLine, 1, 17)))
                ElseIf UCase(Mid(Trim(OBJLine), 1, 17)) = "MESHTEXTURECOORDS" Then
                    'TexCoords = New List(Of Vector2)
                    OBJLine = objReader.ReadLine()
                    s = Split(OBJLine, ";")
                    VCt = CInt(s(0))
                    For i = 0 To VCt - 1
                        OBJLine = objReader.ReadLine()
                        s = Split(OBJLine, ";")
                        vt = New Vector2(CSng(s(0)), CSng(s(1)))
                        TexCoords.Add(vt)
                    Next i
                    GoTo NextSet
                ElseIf UCase(Mid(Trim(OBJLine), 1, 16)) = "MESHMATERIALLIST" Then
                ElseIf UCase(Mid(Trim(OBJLine), 1, 4)) = "MESH" Then
                    If MOCt = -1 Then
                    Else
                        If MO.Indices.Count = 0 Then
                            MOCt = MOCt - 1
                        Else
                            ModObject(MOCt) = MO
                        End If
                    End If
                    MOCt = MOCt + 1
                    ReDim Preserve ModObject(MOCt)
                    MO = New MapObjects.Models.ModelObject
                    MO.GroupName = "Default"
                    For i = 0 To Groups.Count - 1
                        If UCase(MO.GroupName) = UCase(Groups(i)) Then
                            MO.ModelTexture = Textures(i)
                            Exit For
                        End If
                    Next i
                    MO.WorldMatrix = Matrix.Identity
                    MO.MaterialFileName = MTLFile

                    OBJLine = objReader.ReadLine()
                    s = Split(OBJLine, ";")
                    VCt = CInt(s(0))
                    For i = 0 To VCt - 1
                        OBJLine = objReader.ReadLine()
                        s = Split(OBJLine, ";")
                        v = New Vector3(CSng(s(0)) * Scale, CSng(s(1)) * Scale, CSng(s(2)) * Scale)
                        'v = New Vector3(CSng(s(0)), CSng(s(1)), CSng(s(2)))
                        Positions.Add(v)
                    Next i
                    OBJLine = objReader.ReadLine()
                    s = Split(OBJLine, ";")
                    VCt = CInt(s(0))
                    For i = 1 To VCt - 1
                        OBJLine = objReader.ReadLine()
                        s = Split(OBJLine, ";")
                        f = Split(s(1), ",")
                        ID.Vertex = CInt(f(2))
                        ID.TextCoord = CInt(f(2))
                        ID.Normal = -999
                        Indices.Add(ID)
                        ID.Vertex = CInt(f(1))
                        ID.TextCoord = CInt(f(1))
                        ID.Normal = -999
                        Indices.Add(ID)
                        ID.Vertex = CInt(f(0))
                        ID.TextCoord = CInt(f(0))
                        ID.Normal = -999
                        Indices.Add(ID)
                    Next i
                    OBJLine = objReader.ReadLine()
                End If
NextSet:
            Loop
            MO.Positions = Positions
            MO.Indices = Indices
            MO.TexCoords = TexCoords
            MO.Normals = Normals
            objReader = Nothing
            ModObject(MOCt) = MO
        End Sub

    End Class

End Namespace